

import cv2

import time

def VideoControl(framePos):#滑动条回调函数
  global cap
  #cap.set(cv2.cv.CV_CAP_PROP_POS_FRAMES,framePos)#设置视频帧位置OpenCV2.4版本
  cap.set(cv2.CAP_PROP_POS_FRAMES,framePos)#设置视频帧位置OpenCV3版本

##print cv2.__version__

font=cv2.FONT_HERSHEY_COMPLEX_SMALL

cap = cv2.VideoCapture('images/zlh.mp4')
#--纠正注意将对应版本的OpenCV动态库dll目录配置到环境变量
#cap.open('1.avi')

flag = 0

if(cap.isOpened()): #视频打开成功
  flag = 1
##  print flag
else:
  flag = 0
##  print flag
  

cv2.namedWindow('video')

#frameCount = cap.get(cv2.cv.CV_CAP_PROP_FRAME_COUNT)#获取视频总帧数OpenCV2.4版本   
frameCount = cap.get(cv2.CAP_PROP_FRAME_COUNT)#获取视频总帧数OpenCV3版本  
cv2.createTrackbar('framePos','video',0,int(frameCount),VideoControl)

##fps = cap.get(cv2.cv.CV_CAP_PROP_FPS)#获取视频帧率OpenCV2.4版本
fps = cap.get(cv2.CAP_PROP_FPS)#获取视频帧率OpenCV3版本
strFps = "Fps:%0.1f/s"%fps

if(flag==1):
  while(True):
   # position = cap.get(cv2.cv.CV_CAP_PROP_POS_FRAMES)#获取视频帧位置OpenCV2.4版本
    position = cap.get(cv2.CAP_PROP_POS_FRAMES)#获取视频帧位置OpenCV3版本
    cv2.setTrackbarPos("framePos", "video", int(position))#设置滑动条位置
    ret,frame = cap.read()#读取一帧
    #if(frame is None):
    if ret==False: #读取帧失败
      break
    cv2.putText(frame,strFps,(10,20),font,1.1,(0,255,0),2)
    cv2.imshow("video", frame)
    if cv2.waitKey(int(1000/fps))&0xFF ==27: #按下Esc键退出
      break

cap.release()
cv2.destroyAllWindows()
